package com.woniuxy.service.impl;

import com.woniuxy.dao.SkillDao;
import com.woniuxy.dao.UsersDao;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Skill;
import com.woniuxy.entity.User;
import com.woniuxy.entity.dto.UserRoleDto;
import com.woniuxy.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired(required = false)
    UsersDao usersDao;
    @Autowired(required = false)
    SkillDao skillDao;
    //添加用户
    @Override
    public ResponseEntity addUsers(User users) {
        ResponseEntity responseEntity;
        User a = usersDao.findUsers(users.getAccount());
//判断用户是否重复
        if (a != null) {
            return ResponseEntity.error("该账户已存在");
        }
        int num = usersDao.addUsers(users);
        if (num > 0) {
            skillDao.initSkill(users.getId());
            return ResponseEntity.success("成功");
        } else {
            return ResponseEntity.error("失败");
        }
//        ResponseEntity responseEntity;
////        User a=usersDao.findUsers(users.id);
////判断用户是否重复
//        if(a!=null){
//            return ResponseEntity.error("该账户已存在");
//        }
//        int num = usersDao.addUsers(users);
//        if(num>0){
//            return ResponseEntity.success("成功");
//        }else {
//            return  ResponseEntity.error("失败");
//        }

    }

    // 删除 用户
    @Override
    public ResponseEntity delectOne(int id) {
        int delectOne = usersDao.delectOne(id);
        if (delectOne > 0) {
            return ResponseEntity.success("删除成功");
        } else
            return ResponseEntity.error("删除失败");
    }

    //查询
    @Override
    public ResponseEntity findUserAccount(int id) {
        User user = usersDao.findUserAccount(id);
        if (user != null) {
            return ResponseEntity.success("查询成功", user);
        } else {
            return ResponseEntity.error("查询失败");
        }
    }


    //修改
    @Override
    public ResponseEntity updata(User user) {
        int updata = usersDao.updata(user);
        if (updata > 0) {
            return ResponseEntity.success("更新成功");
        } else {
            return ResponseEntity.error("修改失败");
        }

    }

    @Override
    public ResponseEntity findPassword(String account, String password) {
        User findPassword = usersDao.findPassword(account, password);

        System.out.println(findPassword.getPassword());
        if (!findPassword.getPassword().equals(password)) {
            return ResponseEntity.error("密码错误");
        } else {
            return ResponseEntity.success("密码正确");
//            执行修改操作

        }


    }

    @Override
    public ResponseEntity updataPassword(String account, String password) {
        int updataPassword = usersDao.updataPassword(account, password);
        if (updataPassword > 0) {
            return ResponseEntity.success("修改成功");

        } else {
            return ResponseEntity.error("修改失败");
        }

    }

    @Override
    public ResponseEntity updataAvatar(String avatar, String account) {
        int updataAvatar = usersDao.updataAvatar(avatar, account);
        if (updataAvatar > 0) {
            return ResponseEntity.success("修改成功");


        } else {
            return ResponseEntity.error("修改失败");
        }


    }

    @Override
    public ResponseEntity findAllUser() {
        List<UserRoleDto> list = new ArrayList();
        List<Integer> ids = usersDao.findIds();
        for (Integer id : ids) {
            UserRoleDto userRoleDto = new UserRoleDto();
            User user = usersDao.findUser(id);
//            System.out.println(user.toString());
            String role = usersDao.findRole(id);
            userRoleDto.setUser(user);
            userRoleDto.setRole(role);
            list.add(userRoleDto);
            System.out.println(list.toString());

        }
//        System.out.println(list.toString());
        return ResponseEntity.success("success", list);
//        return null;
    }

    @Override
    public ResponseEntity setRole(int roleid, int userid) {
        int isHava = usersDao.findU(userid);
        if (isHava > 0) {
            int num = usersDao.setRole(roleid, userid);
            if (num > 0) {
                return ResponseEntity.success("success");
            } else {
                return ResponseEntity.error("failed");
            }
        } else {
            int num2 = usersDao.setRole2(roleid, userid);
            if (num2 > 0) {
                return ResponseEntity.success("success");
            } else {
                return ResponseEntity.error("failed");
            }
        }
    }


}
